﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;
using gametest.sprites;
using gametest.DxAPI;
using Microsoft.DirectX.DirectDraw;

namespace gametest
{
    class GameLogic
    {
        private enum GameStates { Run, Exit, Pause }
        private GameStates gameState;

        private Control target = null;
        private DxInitGraphics graphics = null;
        private DxKeyboard keyboard = null;
    
        private DxTimer timer = null;

        protected Surface sf1 = null;
        protected Surface sfSecondary = null;
        protected SurfaceDescription sfDesc;
        
        private Background background = null;
        private Background1 background1 = null;
        private RoTrung rt = null;
        private ConGa ga1 = null;
        private trung trung = null;
        private vit vit = null;
        private trung1 trung1 = null;
        private ngan ngan = null;
        private trung2 trung2 = null;

        public GameLogic(Control renderTarget)
        {
            target = renderTarget;
            target.GotFocus += new EventHandler(this.Restore);

            InitDxObjects();
            InitGameObjects();

            gameState = GameStates.Pause;
            GameLoop();
        }

        private void Restore(object sender, EventArgs e)
        {
            graphics = new DxInitGraphics(this.target); // Recreate graphics handler
            background.Restore(graphics);
            background1.Restore(graphics);
            ga1.Restore(graphics);
            trung.Restore(graphics);
            vit.Restore(graphics);
            trung1.Restore(graphics);
            ngan.Restore(graphics);
            trung2.Restore(graphics);
        }

        private void InitDxObjects()
        {
            graphics = new DxInitGraphics(this.target);
            keyboard = new DxKeyboard(this.target);
            timer = new DxTimer();
           
        }

        private void InitGameObjects()
        {
            background = new Background(graphics);
            background1 = new Background1(graphics);
            rt = new RoTrung(graphics);
            ga1 = new ConGa(graphics);
            trung = new trung(graphics, rt);
            vit = new vit(graphics);
            trung1 = new trung1(graphics,rt);
            ngan = new ngan(graphics);
            trung2 = new trung2(graphics,rt);
        }

        private void GameLoop()
        {
            double dLoopDuration = 0.0;

            timer.markTime();
            while (target.Created)
            {
                if (gameState == GameStates.Exit) break;
                if (!target.Focused)
                {
                    System.Threading.Thread.Sleep(100);
                    Application.DoEvents();
                    continue;
                }
      
#if DEBUG
                dLoopDuration += timer.msElapsed();
                if (dLoopDuration > 10.0) // check elapsed milliseconds 
                {
#else 
                    dLoopDuration = timer.msElapsed(); 
#endif
                    //
                    ProcessInput(dLoopDuration);                 
                    UpdateSprites(dLoopDuration);
                    DrawSprites();
#if DEBUG
                    dLoopDuration = 0.0;
                }
#endif
                Application.DoEvents();
            }
            timer.resetTime();
        }

        private void ProcessInput(double dLoopDuration)
        {
            KeyboardState state = keyboard.GetKeyboardState();
      
            if (state != null)
            {
                if (gameState == GameStates.Pause)
                {

                    if (state[Key.D0])
                    {
                        gameState = GameStates.Exit;
                    }

                    if (state[Key.D1])
                    {
                        gameState = GameStates.Run;
                    }
                 
                }
                else if (gameState == GameStates.Run)
                {

                    if (state[Key.Escape])
                    {
                        gameState = GameStates.Pause;
                    }
                    else if (state[Key.Left])
                    {
                        rt.MoveLeft(dLoopDuration);
                    }
                    else if (state[Key.Right])
                    {
                        rt.MoveRight(dLoopDuration);
                    }
               
                }
            }
        }

        private void UpdateSprites(double dLoopDuration)
        {
            if (gameState == GameStates.Run)
            {
               trung.Move(dLoopDuration);
               ga1.MoveGa(dLoopDuration);
               trung1.Move(dLoopDuration);
               trung2.Move(dLoopDuration);
            }
        }

        //DRAW
        private void DrawSprites()
        {
            if (gameState == GameStates.Pause)
            {
                background.Draw();

                Surface sfSecondary = graphics.RenderSurface;
                sfSecondary.ForeColor = (System.Drawing.Color.White);
                sfDesc = new SurfaceDescription();
                sfDesc.SurfaceCaps.OffScreenPlain = true;
                sfSecondary.FontTransparency = true;

                sfSecondary.DrawText(300, 200, "Press 1 to play", false);
                sfSecondary.DrawText(300, 250, "Press 2 to return", false);
                sfSecondary.DrawText(300, 300, "Press 0 to Exit", false);
            }
            else if (gameState == GameStates.Run)
            {
                
                background1.Draw();
                rt.Draw();
                
                trung.Draw();
                ga1.Draw();
                trung1.Draw();
                vit.Draw();
                trung2.Draw();
                ngan.Draw();
            }
            graphics.Flip();
        }
    }
}
